package com.jwt.vpower.view.ui
{
	import TenCMS.components.textbox.textbox;
	import TenCMS.events.BrainEvent;
	import TenCMS.globalvariables;
	
	import com.AppFacade;
	import com.jwt.vpower.model.TenCMS_Components;
	import com.jwt.vpower.view.ui.Button;
	import com.ui.Styles;
	import com.ui.buttons.BaseLink;
	
	import flash.display.Shape;
	import flash.events.*;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class ExternalLinkItem extends Button
	{
		public var _textBox : textbox;
		private var _orgHeight : Number;
		private var _itemID : String
		private var _arrow : Shape;
		public var _hasLink : Boolean;
		public var _link : BaseLink 
		public var _contentxml : XML
		public function ExternalLinkItem()
		{
			super();
			AppFacade.CMSBRAIN.addEventListener(BrainEvent.STATE_CHANGED, onStateChanged);

			
			
			
			_textBox=TenCMS_Components.TextBox(new Rectangle(0,0,137,28),"heading",Styles.DEFAULT_SUB_MENU_ITEM_TITLE);
			
			addChild(_textBox);
			
			
		
			drawArrow();
			this.addEventListener(Event.ADDED_TO_STAGE,onCheck);
			
			
			
		}
		
		private function onCheck($e:Event):void{
			this.removeEventListener(Event.ADDED_TO_STAGE,onCheck);
			addExternalLink();
			onStateChanged()
		}
		override public function onRollOver($e:MouseEvent):void{
			
			doRollOver()
			
		}
		
		override public function onRollOff($e:MouseEvent):void{
			
			if(!selected)doRollOff()
			
		}
		
		public function set itemID($id:String):void{
			_itemID=$id
		}
		
		public function get itemID():String{
			return _itemID;
		}
		override public function doRollOver():void{
			_textBox.textColor=Styles.RED;
			if(_arrow)	_arrow.visible=true
			
		}
		
		override public function doRollOff():void{
			_textBox.textColor=Styles.DARK_GREY;
			if(_arrow)_arrow.visible=false;
			
		}
		
		public function addExternalLink():void{
			_link = new BaseLink("externalquicklinks","external")
			trace(" LINK ADDED " + contentXML);
			this.addChild(_link)
			_link.x=160;
			
			//;
			
		}
		 private function onStateChanged($e:Event=null):void{
			mouseChildren=AppFacade.CMSBRAIN._editingCMS
				trace(" mouseChildren " + mouseChildren)
			_textBox.selectable=AppFacade.CMSBRAIN._editingCMS
			_textBox.wordWrap=AppFacade.CMSBRAIN._editingCMS;
			//	trace(" _link " + _link.visible);
			if(AppFacade.CMSBRAIN._editingCMS){
				
				_textBox.addEventListener(Event.CHANGE,onHandleTextChange);
			}else{
				
				_textBox.removeEventListener(Event.CHANGE,onHandleTextChange);
			}
			if(AppFacade.CMSBRAIN._editingCMS){
				_textBox.width=137
			}else{
				
				if(_textBox.textWidth>137){
					_textBox.width=137;
					_textBox.wordWrap=true;
					_textBox.multiline=true
				}
				_textBox.autoSize=TextFieldAutoSize.LEFT;
			}
			
		}
		public function getHeight():Number{
			return _textBox.textHeight;
		}
		
		public function set contentxml($xml:XML):void{
			_contentxml=$xml;
			_textBox.contentxml=$xml.menuexternallinks.file[0];
			if(_textBox.textWidth>90){
				//trace(" _textBox " + $xml.heading.text)
				_textBox.width=137;
				_textBox.wordWrap=true;
				_textBox.multiline=true
			}else{
				_textBox.wordWrap=false;
			}
			_textBox.autoSize=TextFieldAutoSize.LEFT;
			
			_orgHeight=	_textBox.textHeight;
			
		}
		
		public function get contentxml():XML{
			return _contentxml;
		}
		override public function set contentXML($xml:XML):void{
			
			
			
		}
		
		override public function get contentXML():XML{
			return null;
		}
		
		private function drawArrow():void{
			_arrow = new Shape();
			_arrow.graphics.beginFill(Styles.RED, 1);
			_arrow.graphics.moveTo(20, 10);
			_arrow.graphics.lineTo(20, 10);
			_arrow.graphics.lineTo(27.5, 22.5);
			_arrow.graphics.lineTo(12.5, 22.5);
			_arrow.graphics.endFill();
			_arrow.scaleX= _arrow.scaleY=.5
			_arrow.rotation=90;
			addChild( _arrow);
			_arrow.y= _arrow.y-2
			_arrow.x=5
			_arrow.visible=false;
		}
		
		
		private function onHandleTextChange($e:Event):void{
			trace(" text change");
			if((_textBox.textHeight>_orgHeight)||(_textBox.textHeight<_orgHeight)){
				
				dispatchEvent(new Event("nudge"));
			}
		}
		public function noArrow():void{
			removeChild(_arrow)
			_arrow=null
		}
		
	}
}